from flask import  Flask,request,render_template
import json

app = Flask(__name__)


@app.route('/',methods=['GET','POST'])
def index():
    return render_template('index.html',tanchu = '')

@app.route('/timetable',methods=['GET','POST'])
def timetable():
    with open('./user.json','r') as d:
        user = json.load(d)
    r_fu = request.form['username']
    if r_fu not in user.keys() or request.form['password']!= user[r_fu]['password']:
        tanchu = True #当tanchu = ‘true’时，会弹出alert提示密码错误
        return render_template('index.html',tanchu = tanchu)
    elif r_fu in user.keys() and request.form['password'] == user[r_fu]['password']:
        username = request.form['username']
        return render_template('timetable.html',user = user[username] )

@app.route('/change_password', methods=['GET', 'POST'])
def change_password():
    if request.form['button'] == '修改密码':
        return  render_template('change_password.html',user = request.form['username'],password = request.form['password'])

@app.route('/check', methods=['GET', 'POST'])
def check():
    old_pw = request.form['old_pw']
    new_pw0 = request.form['new_pw0']
    new_pw1 = request.form['new_pw1']
    if old_pw != request.form['password']: #判断输入的旧密码是否与原密码一致
        tanchu = '1'
        return render_template('change_password.html',tanchu = tanchu,user = request.form['user'],password = request.form['password'])
    elif new_pw0 != new_pw1:
        tanchu = '01!=00' #判断前后两次输入的新密码是否一致
        return render_template('change_password.html',tanchu = tanchu,user = request.form['user'],password = request.form['password'])
    elif new_pw0 == old_pw:
        tanchu = 'old=new0' #判断新密码0与旧密码是否相同
        return render_template('change_password.html',tanchu = tanchu,user = request.form['user'],password = request.form['password'])
    elif new_pw1 == old_pw:#判断新密码0与旧密码是否相同
        tanchu = 'old=new1'
        return render_template('change_password.html',tanchu = tanchu,user = request.form['user'],password = request.form['password'])
    elif new_pw0 == new_pw1 and new_pw0 != old_pw and new_pw1 !=old_pw and old_pw == request.form['password']:
        user = request.form['user']
        with open('./user.json','r') as njs:
            params = json.load(njs)
            params[user]['password'] = new_pw1
        with open('./user.json','w') as nnjs:
            json.dump(params,nnjs)
        return render_template('index.html',tanchu = '')

if __name__ == '__main__':
    app.run()
